﻿using System;
using Domain.Core;
using Domain.MainModule.Entities;
using Domain.MainModule.Countries;
using System.Collections.Generic;

namespace Application.MainModule.CountriesManagement
{
    public class CountryManagementService : ICountryManagementService, IDisposable
    {
        #region private properties

        private ICountryRepository CountryRepository
        {
            get;
            set;
        }

        #endregion

        #region constructor

        /// <summary>
        /// Default constructor for CountryService.
        /// </summary>
        /// <param name="employeeRepository">CountryRepository dependency, usually this is resolved by IoC</param>
        public CountryManagementService(ICountryRepository countryRepository)
        {
            if (countryRepository == (ICountryRepository)null)
            {
                throw new ArgumentNullException(Resources.Messages.exception_DependenciesAreNotInitialized);
            }

            this.CountryRepository = countryRepository;
        }

        #endregion

        #region ICountryService implementations

        public void AddCountry(Country country)
        {
            IUnitOfWork unitOfWork = this.CountryRepository.UnitOfWork as IUnitOfWork;
            this.CountryRepository.Add(country);
            unitOfWork.Commit();
        }

        public void ChangeCountry(Country country)
        {
            IUnitOfWork unitOfWork = this.CountryRepository.UnitOfWork as IUnitOfWork;
            this.CountryRepository.Modify(country);
            unitOfWork.CommitAndRefreshChanges();
        }

        public Country FindCountryById(Guid countryId)
        {
            CountryIdSpecification countryIdSpecification = new CountryIdSpecification(countryId);
            return this.CountryRepository.FindCountry(countryIdSpecification);
        }

        public Country FindCountryByName(string countryName)
        {
            CountryNameSpecification countryNameSpecification = new CountryNameSpecification(countryName);
            return this.CountryRepository.FindCountry(countryNameSpecification);
        }

        public Country FindCountryByISONumeric(string isoNumeric)
        {
            CountryISONumericSpecification countryIsoNumericSpecification = new CountryISONumericSpecification(isoNumeric);
            return this.CountryRepository.FindCountry(countryIsoNumericSpecification);
        }

        public Country FindCountryByISOAlpha3(string isoAlpha3)
        {
            CountryISOAlpha3Specification countryIsoAlpha3Specification = new CountryISOAlpha3Specification(isoAlpha3);
            return this.CountryRepository.FindCountry(countryIsoAlpha3Specification);
        }

        public Country FindCountryByISOAlpha2(string isoAlpha2)
        {
            CountryISOAlpha2Specification countryIsoAlpha2Specification = new CountryISOAlpha2Specification(isoAlpha2);
            return this.CountryRepository.FindCountry(countryIsoAlpha2Specification);
        }

        #endregion

        #region IDisposable implementations

        /// <summary>
        /// Dispose associated context with this domain service.
        /// </summary>
        public void Dispose()
        {
            if (this.CountryRepository != null
                &&
                this.CountryRepository.UnitOfWork != null)
            {
                this.CountryRepository.UnitOfWork.Dispose();
            }
        }

        #endregion

        public IEnumerable<Country> GetAll()
        {
            return this.CountryRepository.GetAll();
        }
    }
}
